﻿/***************************************************************************************************
* by Canis, www.clonefactor.com
usage:
var sb:ScrollBar=new ScrollBar( Your_MovieClip );
// var sb:ScrollBar=new ScrollBar( Your_MovieClip, easeAmount, trackColor, grabberColor, grabberPressColor, gripColor, trackThickness, grabberThickness);
// var sb:ScrollBar=new ScrollBar( Your_MovieClip, easeAmount, [TrackDisplayObject], [GrabberDisplayObject]);
sb.setWidth=300;
sb.setHeight=250;
addChild( sb );
sb.toggleFullscreen();	// you can set true/false to force fullscreen or localScreen.
***************************************************************************************************/

package com.cf.core.view.ScrollBar{
	import flash.display.*
	import flash.events.*;
	import com.cf.core.Utils.tools;
	import com.cf.core.Utils.debug;
	import com.cf.core.view.MySprite;
	import com.cf.core.view.ScrollBar.track;
	import com.cf.core.view.ScrollBar.ScrollEvent;
	import com.cf.core.control.SyncSprite;
	
	public class ScrollBar extends Sprite {
		private var G_content:DisplayObjectContainer;
		public function get content():DisplayObjectContainer{return G_content}
		private var G_trackX:track;
		private var G_trackY:track;
		private var G_easeAmount:int;
		private var G_defaultPositionY:Number;
		private var G_defaultPositionX:Number;
		private var G_stageW:Number;
		private var G_stageH:Number;
		private var G_localW:Number;
		private var G_localH:Number;
		public function set setWidth(_n:Number):void{G_localW=_n}
		public function set setHeight(_n:Number):void{G_localH=_n}
		private var G_alignCenter:Boolean=true;
		public function set alignCenter(_f:Boolean):void{G_alignCenter=_f}
		public function get alignCenter():Boolean{return G_alignCenter}
		public function get scrollX():Number{return (-1*G_content.x);}//fn
		public function get scrollY():Number{return (-1*G_content.y);}//fn
		private var G_fullscreen:Boolean;
		private var G_mask:MySprite;
		private var G_bg:MySprite;
		private var G_gui:SyncSprite;
		public function get ui():Sprite{return G_gui as Sprite}
		
		// Call style 1
		// ScrollBar(_Display, easeAmount, trackColor, grabberColor, grabberPressColor, gripColor, trackThickness, grabberThickness);
		//
		// Call style 2
		// ScrollBar(_Display, easeAmount, TrackObj_y, GrabberObj_y);
		//
		public function ScrollBar(c:DisplayObjectContainer, ea:int=5, tc:*=0x666666, gc:*=0xCCCCCC, gpc:uint=0xFFFFFF, grip:uint=0x999999, tt:int=14, gt:int=14){
			G_content = c;
			G_trackX = new track(c, tc, gc, gpc, grip, tt, gt);
			G_trackY = new track(c, tc, gc, gpc, grip, tt, gt);
			G_trackX.checkWidth=true;
			G_trackY.checkHeight=true;
			G_easeAmount = ea;
			init();
		}//fn
		
		private function init():void{
			setWidth=100;
			setHeight=100;
			G_gui=new SyncSprite();
			G_mask=new MySprite();
			G_bg=new MySprite();
			G_bg.alpha=0;
			G_content.mask = G_mask;
			G_gui.mask = G_mask;
			G_trackX.rotation = 270;
			addChild(G_bg);
			addChild(G_content);
			addChild(G_gui);
			addChild(G_trackX);
			addChild(G_trackY);
			addChild(G_mask);
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
			G_defaultPositionY = Math.round(G_content.y);
			G_defaultPositionX = Math.round(G_content.x);
			G_fullscreen=false;
			// trace("ScrollBar :  local width "+this.width+", height "+ this.height);
		}//fn
		
		private function stopScroll(e:Event):void{
			onUpListener();
		}//fn
		private function scrollContentX(e:ScrollEvent):void{
			if(G_trackX.visible) G_content.x = (G_mask.width-G_content.width)*(e.data.value/100);
			dispatchEvent(new ScrollEvent(ScrollEvent.CHANGE, {x:G_content.x, y:G_content.y, content:G_content}));
		}//fn
		private function scrollContentY(e:ScrollEvent):void{
			if(G_trackY.visible) G_content.y = (G_mask.height-G_content.height)*(e.data.value/100);
			dispatchEvent(new ScrollEvent(ScrollEvent.CHANGE, {x:G_content.x, y:G_content.y, content:G_content}));
		}//fn
		public function toggleFullscreen(_fg:*=null):void{
			// local fullscreen
			if(_fg==null) G_fullscreen=!G_fullscreen;
			if(_fg is Boolean) G_fullscreen=_fg;
			reflesh();
			if(G_trackX.visible) G_trackX.value=G_trackX.value;
			if(G_trackY.visible) G_trackY.value=G_trackY.value;
		}//fn
		public function reflesh():void{
			if( G_fullscreen ){
				G_trackX.length = G_stageW;
				G_trackY.length = G_stageH;
				if( G_trackX.visible && G_trackY.visible) G_trackX.length=G_stageW-G_trackY.width;
				G_mask.Rect(0,0, G_stageW, G_stageH);
			}else{
				G_trackX.length = G_localW;
				G_trackY.length = G_localH;
				if( G_trackX.visible && G_trackY.visible){
					G_trackX.length=G_localW-G_trackY.width;
				}//if
				G_mask.Rect(0,0, G_localW, G_localH);
				G_bg.Rect(0,0, G_localW, G_localH);
			}//if
			G_trackY.x = G_mask.width - G_trackY.width;
			G_trackX.y = G_mask.height;
			if(this.alignCenter){
				if( G_mask.width > G_content.width ){
					G_content.x = (G_mask.width/2)-(G_content.width/2);
				}//if
				if( G_mask.height > G_content.height ){
					G_content.y = (G_mask.height/2)-(G_content.height/2);
				}//if
			}//if
			// trace("ScrollBar : reflesh x:"+ G_trackX.length + " y:"+ G_trackY.length );
		}//fn
		
		
		//============================================================================================================================
		// LISTENERS
		//============================================================================================================================
		private function onUpListener(e:MouseEvent = null):void{
			G_trackX.stopMove();
			G_trackY.stopMove();
		}//fn
		private function mouseWheelListener(me:MouseEvent):void{
			// Effect on axia Y only
			//*****
			var d:Number = me.delta;
			if(G_trackY.visible){
				G_trackY.value -= d;
			}//if
			trace("Wheel event "+ G_trackY.value +"["+(d*4)+"]");
			//*****/
		}//fn
		public function RELEASE(e:*=null):void{
			tools.emptySprite(G_gui);
			removeEventListener(Event.REMOVED_FROM_STAGE, RELEASE);
			removeEventListener(Event.ENTER_FRAME, updateTrackListener);
			stage.removeEventListener(Event.RESIZE, onStageResize);
			stage.removeEventListener(Event.MOUSE_LEAVE, stopScroll);
			removeEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelListener);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onUpListener);
			G_trackX.removeEventListener(ScrollEvent.CHANGE, scrollContentX);
			G_trackY.removeEventListener(ScrollEvent.CHANGE, scrollContentY);
		}//fn
		private function onAddedToStage(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, RELEASE);
			addEventListener(Event.ENTER_FRAME, updateTrackListener, false, 0, true);
			stage.addEventListener(Event.RESIZE, onStageResize, false, 0, true);
			stage.addEventListener(Event.MOUSE_LEAVE, stopScroll);
			addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelListener);
			stage.addEventListener(MouseEvent.MOUSE_UP, onUpListener);
			G_trackX.addEventListener(ScrollEvent.CHANGE, scrollContentX, false, 0, true);
			G_trackY.addEventListener(ScrollEvent.CHANGE, scrollContentY, false, 0, true);
			onStageResize();
			G_gui.syncFrom(G_content);
			G_gui.syncX=true;
			G_gui.syncY=true;
			G_gui.syncScaleX=true;
			G_gui.syncScaleY=true;
		}//fn
		private function updateTrackListener(e:Event=null):void{
			reflesh();
		}//fn
		private function onStageResize(e:Event=null):void{
			G_stageW = stage.stageWidth;
			G_stageH = stage.stageHeight;
			// reflesh();
		}//fn
	}//class
}//package